<?php

/**
 * 
 * @author yuxun
 *
 */
class ApiCrossSynchro{
    
    public function request($apiType, $params = array()){
    	$api_key = Yii::app()->params['API_CROSS_SYNCHRO_KEY'];
    	$api_url = Yii::app()->params['API_CROSS_SYNCHRO_URL'];
    	
    	$url = $api_url . $apiType;
    	$sign = md5($api_key.date('Ymd',time()));
//     	echo $api_key;echo '<br/>';
//     	echo $api_key.date('Ymd',time());echo '<br/>';
//     	echo $sign;echo '<br/>';
    	// 记录请求日志
    	Yii::log($url, CLogger::LEVEL_INFO, 'request.url');

    	if(count($params) != 0){
    		$params = array_merge($params, array(
    				'sign' => $sign
    		));
    		$p_json = json_encode($params);

    		$re = self::curl_post($url, $p_json);
    	}else{
    		$re = self::curl_get($url);
    	}

    	self::doLog($user, $apiType, $params, $url, $re);
    	
    	if($re['code'] != 200){
    		// 如果http返回码不是200,则返回请求失败
    		return array('state' => false, 'msg' => '请求失败', 'origin_result' => $re);
    	}else{
    		$tmp = json_decode($re['result'], true);
    		$returnCode = $tmp['code'];
    	
    		// 如果成功,则格式化数据返回
    		if(isset($returnCode) && ($returnCode == 0)){
    			$return = array('state' => true);
    	
    			// 可选返回参数
    			$returnArr = array('data');
    			foreach($returnArr as $v){
    				if(isset($tmp[$v])){
    					$return[$v] = $tmp[$v];
    				}else if($v == 'data'){
    					$return['data'] = array();
    				}
    			}
    	
    			// 请求的原始返回
    			$return['origin_result'] = $re['result'];
    	
    			return $return;
    		}else{
    			return array('state' => false, 'msg' => $tmp['msg'], 'origin_result' => $re['result']);
    		}
    	}

    }
    
    function curl_get($url)
    {
    	$ch = curl_init();
    	curl_setopt($ch, CURLOPT_URL, $url);
    	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    	
    	$result['result'] = curl_exec($ch);
    	
    	$result['code'] = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    	
    	if ($result['result'] === false) {
    		$result['result'] = curl_error($ch);
    		$result['code'] = -curl_errno($ch);
    	}
    	
    	curl_close($ch);
    	return $result;
    }
    
    function curl_post($url, $data_string) {
    	$ch = curl_init();
    	curl_setopt($ch, CURLOPT_URL, $url);
    	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    
    	curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    	'X-AjaxPro-Method:ShowList',
    	'Content-Type: application/json; charset=utf-8',
    	'Content-Length: ' . strlen($data_string))
    	);
    	curl_setopt($ch, CURLOPT_POST, 1);
    	curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
    	
    	$result['result'] = curl_exec($ch);
    	 
    	$result['code'] = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    	 
    	if ($result['result'] === false) {
    		$result['result'] = curl_error($ch);
    		$result['code'] = -curl_errno($ch);
    	}
    	 
    	curl_close($ch);
    	return $result;
    }
    
    public function doLog($user, $apiType, $params, $url, $re){
    	$array = array(
    			'user_id'       => $user['UserID'],
    			'account'       => $user['Account'],
    			'user_name'     => $user['UserName'],
    			'type'          => $apiType,
    			'params'        => is_array($params) ? json_encode($params) : $params,
    			'url'           => $url,
    			'return'        => is_array($re) ? json_encode($re) : $re,
    			'create_time'   => time()
    	);
    
    	$model = new ModApiLog();
    	$model->setAttributes($array, false);
    	$model->save();
    }
    
    /**
     * 发送跨服数据
     */
    public function call($params){
    	return $this->request('', $params);
    }


}